<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="任务" prop="taskId">
      <el-input v-model="dataForm.taskId" placeholder="任务"></el-input>
    </el-form-item>
    <el-form-item label="用户" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="用户"></el-input>
    </el-form-item>
    <el-form-item label="上报状态" prop="reportStatus">
      <el-input v-model="dataForm.reportStatus" placeholder="上报状态"></el-input>
    </el-form-item>
    <el-form-item label="填报完成度" prop="reportProgress">
      <el-input v-model="dataForm.reportProgress" placeholder="填报完成度"></el-input>
    </el-form-item>
    <el-form-item label="填报时间" prop="reportTime">
      <el-input v-model="dataForm.reportTime" placeholder="填报时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          taskId: '',
          userId: '',
          reportStatus: '',
          reportProgress: '',
          reportTime: ''
        },
        dataRule: {
          taskId: [
            { required: true, message: '任务不能为空', trigger: 'blur' }
          ],
          userId: [
            { required: true, message: '用户不能为空', trigger: 'blur' }
          ],
          reportStatus: [
            { required: true, message: '上报状态不能为空', trigger: 'blur' }
          ],
          reportProgress: [
            { required: true, message: '填报完成度不能为空', trigger: 'blur' }
          ],
          reportTime: [
            { required: true, message: '填报时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/frt/taskuser/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.taskId = data.taskuser.taskId
                this.dataForm.userId = data.taskuser.userId
                this.dataForm.reportStatus = data.taskuser.reportStatus
                this.dataForm.reportProgress = data.taskuser.reportProgress
                this.dataForm.reportTime = data.taskuser.reportTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/frt/taskuser/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'taskId': this.dataForm.taskId,
                'userId': this.dataForm.userId,
                'reportStatus': this.dataForm.reportStatus,
                'reportProgress': this.dataForm.reportProgress,
                'reportTime': this.dataForm.reportTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
